package cn.iocoder.yudao.module.wms.service.batch;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.batch.vo.BatchPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.batch.vo.BatchSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.batch.BatchDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.inventory.InventoryDO;
import cn.iocoder.yudao.module.wms.dal.mysql.batch.BatchMapper;
import cn.iocoder.yudao.module.wms.dal.redis.serialNumber.WmsSerialNumberRedisDAO;
import cn.iocoder.yudao.module.wms.enums.inventory.IsBatch;
import cn.iocoder.yudao.module.wms.service.inventory.InventoryServiceImpl;
import cn.iocoder.yudao.module.wms.service.stock.StockServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.BATCH_NOT_EXISTS;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.BATCH_SERIAL_EXISTS;

/**
 * 批号设置 Service 实现类
 *
 * @author 曾劲松
 */
@Service
@Validated
public class BatchServiceImpl implements BatchService{

    @Resource
    private BatchMapper batchMapper;
    @Resource
    private WmsSerialNumberRedisDAO serialNumberRedisDAO;
    @Resource
    private StockServiceImpl stockService;
    @Resource
    private InventoryServiceImpl inventoryService;

    @Override
    public Long createBatch(BatchSaveReqVO createReqVO) {
        // 插入
        BatchDO batch = BeanUtils.toBean(createReqVO, BatchDO.class);
        batchMapper.insert(batch);
        // 返回
        return batch.getId();
    }

    @Override
    public void updateBatch(BatchSaveReqVO updateReqVO) {
        // 校验存在
        validateBatchExists(updateReqVO.getId());
        // 更新
        BatchDO updateObj = BeanUtils.toBean(updateReqVO, BatchDO.class);
        batchMapper.updateById(updateObj);
    }

    @Override
    public void deleteBatch(Long id) {
        // 校验存在
        validateBatchExists(id);
        // 删除
        batchMapper.deleteById(id);
    }

    private void validateBatchExists(Long id) {
        if (batchMapper.selectById(id) == null) {
            throw exception(BATCH_NOT_EXISTS);
        }
    }

    @Override
    public BatchDO getBatch(Long id) {
        return batchMapper.selectById(id);
    }

    @Override
    public PageResult<BatchDO> getBatchPage(BatchPageReqVO pageReqVO) {
        return batchMapper.selectPage(pageReqVO);
    }

    @Override
    public String generateBatch(Long inventoryId) {
        InventoryDO inventory = inventoryService.getInventory(inventoryId);
        //判断是否启用批次
        if (inventory.getIsBatch().equals(IsBatch.ENABLE.getIsBatch())) {
            BatchDO batchDO = batchMapper.selectOnlyOne();
            String batch = serialNumberRedisDAO.generate(batchDO.getFname(), batchDO.getTname(), batchDO.getSname());
            if (stockService.findBatchCodeDuplicate(batch) != 0) {
                throw exception(BATCH_SERIAL_EXISTS);
            }
            return batch;
        } else {
            return "";
        }
    }

}